// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
const ipc = require("electron").ipcRenderer;
var app = new Vue({
  el: "#app",
  data: {
    message: "Hello Vue!",
    mapinfo: {
      start: "",
      end: "",
      startText: "起点位置",
      endText: "终点位置",
      startLocation: [],
      endLocation: [],
    },
    map: undefined,
    loadingInstance: undefined,
  },
  created: function () {
    ipc.on("xlsx-select", (channel,data) => {
      console.log(data);
    });
  },
  mounted: function () {
    this.loadingInstance = this.$loading();
    var map = new AMap.Map("container", {
      resizeEnable: true,
      center: [116.397428, 39.90923], //地图中心点
      zoom: 5, //地图显示的缩放级别
    });
    this.map = map;

    //输入提示
    var autoStart = new AMap.Autocomplete({
      input: "start",
      city: "全国",
    });

    //输入提示
    var autoEnd = new AMap.Autocomplete({
      input: "end",
      city: "全国",
    });
    var placeSearch = new AMap.PlaceSearch({
      map: map,
    }); //构造地点查询类
    AMap.event.addListener(autoStart, "select", startSelect); //注册监听，当选中某条记录时会触发
    AMap.event.addListener(autoEnd, "select", endSelect); //注册监听，当选中某条记录时会触发
    AMap.event.addListener(placeSearch, "markerClick", markerClick); //注册监听，当选中某条记录时会触发

    var isStart = false;

    var mapinfo = this.mapinfo;
    function startSelect(e) {
      placeSearch.setCity(e.poi.adcode);
      placeSearch.search(e.poi.name); //关键字查询查询
      isStart = true;
    }
    function endSelect(e) {
      placeSearch.setCity(e.poi.adcode);
      placeSearch.search(e.poi.name); //关键字查询查询
      isStart = false;
    }
    function markerClick(e) {
      console.log(e);
      if (isStart) {
        mapinfo.startText = e.data.name;
        mapinfo.startLocation = [e.data.location.lng, e.data.location.lat];
      } else {
        mapinfo.endText = e.data.name;
        mapinfo.endLocation = [e.data.location.lng, e.data.location.lat];
      }
    }

    /**
     * Created with WebStorm && Sublime Text 3
     * Date: 2015/10/29 14:04
     */
    window.onload = function () {
      map.plugin(["AMap.ToolBar"], function () {
        map.addControl(new AMap.ToolBar());
      });
      if (location.href.indexOf("&guide=1") !== -1) {
        map.setStatus({ scrollWheel: false });
      }
    };
    this.loadingInstance.close();
  },
  methods: {
    reset: function () {
      location.reload();
    },
    onSubmit: async () => {
      const data = await ipc.send("open-file");
      console.log(data);
    },
  },
});
